#include "bsp_uart.h"



void bsp_uart_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure = {0};
	/*UART-GPIO init*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);

	/*TX----PA9*/
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	/*RX----PA10*/
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	/*UART init*/
	USART_InitTypeDef USART_InitStructure = {0};
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);
	
	/*interrupt config*/
//	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

	/*启动*/
	USART_Cmd(USART1, ENABLE);
}

void bsp_uart_deinit(void)
{

}

/*
* 
*/
void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}

void uart1_putc(char c)
{
	uint8_t timeout = 0xFF;
	USART_SendData(USART1, c);
	do {
		timeout--;
		if(USART_GetFlagStatus(USART1, USART_FLAG_TC) == SET) {
			/*发送完毕*/
			break;
		}
	}while(timeout != 0);
	
}

int uart1_puts(char *s)
{
	int count = 0;
	while(*s != 0){
		count++;
		uart1_putc(*s);
		s++;
	}
	return count;
}
